package com.atguigu.tingshu.search.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.search.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 饭勺
 * @description 在微服务中添加监听方法
 * @createDate 2025/9/28 20:52
 */
@Slf4j
@Component
public class SearchReceiver {

    @Autowired
    private SearchService searchService;

    /**
     * @Exchange(autoDelete="true当最后一个绑定到Exchange上的队列删除后，自动删除该Exchange")
     * @Queue(autoDelete="true没有消费者队列就会自动删除")
     *
     * 监听专辑上架队列，完成专辑上架
     * @param albumId 专辑ID
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM,durable = "true",autoDelete = "true"),
            value = @Queue(value = MqConst.QUEUE_ALBUM_UPPER,durable = "true",autoDelete = "true"),
            key = MqConst.ROUTING_ALBUM_UPPER
    ))
    public void albumUpper(Long albumId, Message message, Channel channel) {
        if (albumId != null){
            log.info("[搜索服务]监听到专辑上架消息：{}",albumId);
            searchService.upperAlbum(albumId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
